// js/weather.js

function createRainEffect() {
    const particles = 5000;
    const geometry = new THREE.BufferGeometry();
    const positions = [];

    for (let i = 0; i < particles; i++) {
        positions.push(
            Math.random() * 200 - 100, // x
            Math.random() * 100,      // y
            Math.random() * 200 - 100  // z
        );
    }
    geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));

    const material = new THREE.PointsMaterial({
        color: 0xaaaaaa,
        size: 0.2,
        transparent: true,
        opacity: 0.7
    });

    const rain = new THREE.Points(geometry, material);
    rain.name = 'rainEffect';

    // Add a simple animation function
    rain.animate = function() {
        const positions = this.geometry.attributes.position.array;
        for (let i = 1; i < positions.length; i += 3) {
            positions[i] -= 0.3; // Move down
            if (positions[i] < 0) {
                positions[i] = 100; // Reset to top
            }
        }
        this.geometry.attributes.position.needsUpdate = true;
    };
    
    return rain;
}

function createSnowEffect() {
    const particles = 10000;
    const geometry = new THREE.BufferGeometry();
    const positions = [];
    const velocities = [];

    for (let i = 0; i < particles; i++) {
        positions.push(
            Math.random() * 200 - 100,
            Math.random() * 100,
            Math.random() * 200 - 100
        );
        velocities.push(
            (Math.random() - 0.5) * 0.1, // x-velocity
            -0.05 - Math.random() * 0.05, // y-velocity
            (Math.random() - 0.5) * 0.1  // z-velocity
        );
    }
    geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
    geometry.setAttribute('velocity', new THREE.Float32BufferAttribute(velocities, 3));

    const material = new THREE.PointsMaterial({
        color: 0xffffff,
        size: 0.3,
        transparent: true,
        opacity: 0.8,
        map: createSnowflakeTexture(),
        blending: THREE.AdditiveBlending,
        depthWrite: false
    });

    const snow = new THREE.Points(geometry, material);
    snow.name = 'snowEffect';
    
    snow.animate = function() {
        const positions = this.geometry.attributes.position.array;
        const velocities = this.geometry.attributes.velocity.array;

        for (let i = 0; i < positions.length; i+=3) {
            positions[i] += velocities[i];
            positions[i+1] += velocities[i+1];
            positions[i+2] += velocities[i+2];

            // Reset when snow falls out of view
            if (positions[i+1] < 0) {
                positions[i] = Math.random() * 200 - 100;
                positions[i+1] = 100;
                positions[i+2] = Math.random() * 200 - 100;
            }
        }
        this.geometry.attributes.position.needsUpdate = true;
    };

    return snow;
}

function createSnowflakeTexture() {
    const canvas = document.createElement('canvas');
    canvas.width = 32;
    canvas.height = 32;
    const context = canvas.getContext('2d');
    const gradient = context.createRadialGradient(16, 16, 0, 16, 16, 16);
    gradient.addColorStop(0, 'rgba(255,255,255,1)');
    gradient.addColorStop(0.2, 'rgba(255,255,255,0.8)');
    gradient.addColorStop(0.4, 'rgba(255,255,255,0.3)');
    gradient.addColorStop(1, 'rgba(255,255,255,0)');
    context.fillStyle = gradient;
    context.fillRect(0, 0, 32, 32);
    return new THREE.CanvasTexture(canvas);
} 